<?php

namespace Home\Model\Ajax;

class MobileLoginCodeVerify extends AbstractAjax
{
    // 用手机验证码登录，正确直接通过手机号登录
    public function handle()
    {
        $code = I('request.code', '', 'trim');
        $mobile = I('request.mobile', '', 'trim');
        $auto = I('request.auto', '', 'intval');
        if (session('mobile_code') == '') {
            $ajax['status'] = 0;
            $ajax['content'] = "请先发送验证码！";
            return $ajax;
        }
        if (empty($code)) {
            $ajax['status'] = 0;
            $ajax['content'] = "验证码不能为空！";
            return $ajax;
        }
        if (session('mobile_code') == '') {
            $ajax['status'] = 0;
            $ajax['content'] = "验证码失效，请重新发送！";
            return $ajax;
        }
        if (session('mobile_post') <> $mobile) {
            $ajax['status'] = 0;
            $ajax['content'] = "提交的手机号和发送验证码的手机号不一致";
            return $ajax;
        }
        if ($code == session('mobile_code')) {
            D('User')->user_login('', $mobile, 2);
            if ($auto == '1') {//设置cookie
                $user = D('User')->get_user_one($mobile, false, 2);
                cookie('ytata_username', $user['username'], 31536000);
                cookie('ytata_password', $user['password'], 31536000);
            }
            $ajax['status'] = 1;
            $ajax['content'] = "验证通过！";
            return $ajax;
        } else {
            $ajax['status'] = 0;
            $ajax['content'] = "验证码错误，请正确填写！";
            return $ajax;
        }
    }
}